import os

import cv2

'''
    path    原文件文件夹路径
    newPath   压缩好的文件文件夹路径
    prefix   压缩好后的文件前缀
    fps   fps
    notresizepre  跳过开头多少秒
    notresizeend   跳过结尾多少秒
    w_h_multiple   长宽压缩倍数
    repeat_fps   每帧重复次数
'''


def resize_video(path, newPath, prefix, fps, notresizepre, notresizeend, w_h_multiple, jump_fps,repeat_fps):
    if not os.path.exists(newPath):
        os.makedirs(newPath)
    for dirpath, dirnames, filenames in os.walk(path):
        count = 1
        for filename in filenames:
            namecount = len(filenames)
            try:
                cam1 = cv2.VideoCapture(os.path.join(path, filename))
                # 获取视频1的宽度
                ww = int(cam1.get(3))
                # 获取视频1的高度
                hh = int(cam1.get(4))
                print(ww, hh)
                # 获取视频的帧频
                CAMERA_FPS = cam1.get(cv2.CAP_PROP_FPS)  # 25帧/秒
                print('帧频:' + str(CAMERA_FPS))
                # 获取视频的帧数
                CAP_PROP_FRAME_COUNT = cam1.get(cv2.CAP_PROP_FRAME_COUNT)  # 25帧/秒
                print("帧数：" + str(CAP_PROP_FRAME_COUNT))
                # 定义视频编码格式
                fourcc = cv2.VideoWriter_fourcc(*'mp4v')
                # 创建视频保存对象
                videoWriter = cv2.VideoWriter(os.path.join(newPath, prefix + filename[:-4]+'.mp4'), fourcc, fps,
                                              (int(ww * w_h_multiple), int(hh * w_h_multiple)))
                # videoWriter = cv2.VideoWriter('C:/Users/Administrator/Desktop/out.wmv', fourcc, CAMERA_FPS,(ww, hh*2))
                num = 0
                while True:
                    # 视频最后一秒不参与合并到新视频
                    if CAMERA_FPS * notresizepre <= num <= CAP_PROP_FRAME_COUNT - CAMERA_FPS * notresizeend:
                        # if CAMERA_FPS * notresizepre <= num <= CAMERA_FPS * (notresizepre+10):
                        # 读取指定num帧数对应视频的帧
                        cam1.set(cv2.CAP_PROP_POS_FRAMES, num)
                        (ok1, frame1) = cam1.read()
                        if ok1:
                            # 重置视频大小，使两视频大小一致
                            if w_h_multiple != 1:
                                frame1 = cv2.resize(frame1, (int(ww * w_h_multiple), int(hh * w_h_multiple)))
                            # # 拼接处理
                            # image = np.concatenate([frame1, frame1, frame1], axis=1)  # axis=0时为垂直拼接；axis=1时为水平拼接
                            # 视频保存
                            # videoWriter.write(image)
                            for i in range(repeat_fps):
                                videoWriter.write(frame1)
                            if cv2.waitKey(1) & 0xFF == ord('q'):
                                break
                        else:
                            break
                    elif num > CAP_PROP_FRAME_COUNT - CAMERA_FPS * notresizeend:
                        print(num)
                        break
                    print("文件总数{}，当前完成视频 {} 为第{}个 {} / {}".format(namecount, filename, count, num, CAP_PROP_FRAME_COUNT))
                    num = num + jump_fps + 1
                videoWriter.release()
                cam1.release()
                cv2.destroyAllWindows()
            except Exception as e:
                print(e)
                continue
            count += 1

if __name__ == '__main__':
    # path = r'D:\beautyleg-1'  # 原文件文件夹路径
    path = r'D:\bilibilivideobypy\xuya-yaojiwu'  # 原文件文件夹路径
    # newPath = r'D:\beautyleg-resize'  # 压缩好的文件文件夹路径
    newPath = r'D:\bilibilivideobypy\xuya-yaojiwu-resize1'  # 压缩好的文件文件夹路径
    prefix = 'resize-'  # 压缩好后的文件前缀
    fps = 5  # fps
    notresizepre = 30  # 跳过开头多少秒
    notresizeend = 30  # 跳过结尾多少秒
    w_h_multiple = 1  # 长宽压缩倍数
    jump_fps = 0  # 跳过帧数
    repeat_fps = 4  # 跳过帧数
    resize_video(path, newPath, prefix, fps, notresizepre, notresizeend, w_h_multiple, jump_fps,repeat_fps)
